import 'package:flutter/services.dart';
import 'package:flutter_module/beans/data_layer_bean.dart';
import 'package:flutter_module/beans/element_bean.dart';
import 'package:flutter_module/map/models/ni_geopoint_model.dart';
import 'package:flutter_module/system/log_util.dart';
import 'package:fluttertoast/fluttertoast.dart';

import 'ni_data_method_id.dart';

class NIDataElementDispatcher {
  static const _tag = 'NIDataElementDispatcher';

  ///创建要素数据
  Future<String?> saveElementData(
      MethodChannel dataChannel, ElementBean elementBean) async {
    LogUtil.instance.printf("${elementBean.toMap()}");
    var res = await dataChannel.invokeMethod(
        NIDataElementMethodId.kDataSaveElementData, elementBean.toMap());
    LogUtil.instance.printf("$res");
    return res;
  }

  Future<Map?> importHDPbfData(
      MethodChannel _dataChannel, List<String?> fileList) async {
    for (var element in fileList) {
      LogUtil.instance.printf(element ?? "空目录");
    }
    var res = await _dataChannel.invokeMethod(
        HDDataMethodId.kImportPbfData, fileList);
    LogUtil.instance.printf("$res");
    return res;
  }

  ///捕捉数据
  ///[list]要捕捉的坐标点的外接矩形坐标
  ///[wkt]  捕捉范围的几何 wkt格式 POLYGON((),())
  Future<List<ElementBean>> snapDataList(
      MethodChannel dataChannel, String wkt) async {
    var res = await dataChannel.invokeMethod(
        NIDataElementMethodId.kDataSnapElementDataList, wkt);
    List<ElementBean> newList = [];
    if (res is List) {
      for (var map in res) {
        if (map is Map) {
          newList.add(ElementBean.fromMap(map));
        }
      }
    }
    return newList;
  }

  Future<DataLayer?> queryElementDeepInfo(
      MethodChannel dataChannel, String id, String layerId) async {
    var res = await dataChannel.invokeMethod(
        NIDataElementMethodId.kDataQueryElementDeepInfo,
        {"id": id, "layerId": layerId});
    if (res is Map) {
      return DataLayer.from(res);
    } else {
      Fluttertoast.showToast(msg: "$res");
    }
  }
}
